home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / pascal / gsdbloo.exe / GS_DBASE.PAS < prev    next >
Pascal/Delphi Source File  |  1992-02-28  |  53KB  |  1,126 lines

  1. {-----------------------------------------------------------------------------
  2.                            dBase III File Handler
  3.  
  4.        GS_DBASE Copyright (c)  Richard F. Griffin
  5.  
  6.        15 November 1990
  7.  
  8.        102 Molded Stone Pl
  9.        Warner Robins, GA  31088
  10.  
  11.        -------------------------------------------------------------
  12.        This unit handles the objects for all dBase III file (.DBF)
  13.        operations.
  14.  
  15.                    SHAREWARE  -- COMMERCIAL USE RESTRICTED
  16.  
  17.  
  18.  
  19.        Changes:
  20.  
  21.        16 Nov 90 - Moved Pack method to GS_dBFld.
  22.  
  23.        02 May 91 - Added an IndexSignature constant to the index units so the
  24.                    GS_dBase unit can confirm the index unit in use.  The flag
  25.                    IsDB3NDX is true if the dBase III index unit is used.  This
  26.                    is needed to properly convert date fields for an index.  The
  27.                    dBase III index requires a julian date instead of the
  28.                    character field stored in the record.  Most other indexes
  29.                    use the field as stored (YYYYMMDD).
  30.  
  31.        03 May 91 - Added routine to convert a date field to julian date when
  32.                    used as an index field in PutRec.
  33.  
  34.        06 Jun 91 - Fixed error in Open that caused the status not to be set
  35.                    to 'NotUpdated'.  Comment close bracket was missing, and
  36.                    caused the next instruction to be ignorred.
  37.  
  38.                    Added a UnInit method to release buffer memory from the
  39.                    Heap when the file is no longer needed.  If the file is
  40.                    to be used again, it must be reinitialized by calling
  41.                    the Init method.  This allows several files to use the
  42.                    same object, one after the other.
  43.  
  44.        08 Jan 92 - Changed UnInit to a destructor to make creating and
  45.                    disposing of dynamic objects possible.
  46.  
  47.                    Added GSP_dBase_DB as a pointer of type GS_dBase_DB for
  48.                    simpler creation of dynamic objects.
  49.  
  50.        02 Feb 92 - Allows multiple indexes to be updated through PutRec.
  51.                    This will slow the PutRec function as a penalty.
  52.  
  53.        18 Feb 92 - Added File_TOF flag to test for attempt to read beyond
  54.                    the top of the file.  Use like File_EOF test.
  55.  
  56.        27 Feb 92 - Fix made to allow easy change of the master index.
  57.                    Modified dbfNdxActv from boolean value to GS_Indx_LPtr
  58.                    index object pointer.  This will hold the master index
  59.                    to be used for GetRec and Find.  Will default to the
  60.                    first file in an Index command.  May be set to other
  61.                    index files by calling the SetIndexMaster procedure
  62.                    with the order number of the index as the argument.
  63.  
  64.        28 Feb 92 - Added FieldName method to return the name of the field
  65.                    in the record at the ordered position.  For example,
  66.                    FieldName(2) would return the name of the second field
  67.                    in the record.
  68.  
  69. ------------------------------------------------------------------------------}
  70. {
  71.                            ┌──────────────────────┐
  72.                            │  INTERFACE SECTION:  │
  73.                            └──────────────────────┘
  74. }
  75.  
  76. unit GS_DBASE;
  77.  
  78. interface
  79. {$D-}
  80.  
  81. uses
  82.      CRT,
  83.      DOS,
  84.      GS_KeyI,
  85.      GS_Date,
  86.      GS_FileH,    {File handler}
  87.      GS_Strng,    {String handling Routines}
  88.      GS_Error,    {Error Handling routines}
  89.      GS_DBNdx;    {Unit for index operations (.NDX files)}
  90.  
  91. const
  92.    GS_dBase_MaxRecBytes = 4000;      {dBASE III record limit }
  93.    GS_dBase_MaxRecField = 128;       {dBASE III field limit}
  94.    GS_dBase_MaxMemoRec  = 512;       {Size of each block of memo file data}
  95.  
  96.    Next_Record = -1;   {Token value passed to read next record}
  97.    Prev_Record = -2;   {Token value passed to read previous record}
  98.    Top_Record  = -3;   {Token value passed to read first record}
  99.    Bttm_Record = -4;   {Token value passed to read final record}
  100.  
  101.    GS_dBase_UnDltChr = 32;   {Character for Undeleted Record}
  102.    GS_dBase_DltChr   = 42;   {Character for Deleted Record}
  103.  
  104. type
  105.  
  106.    GS_dBase_Status = (NotOpen, NotUpdated, Updated);
  107.            {Flags to indicate status of dBase III file }
  108.  
  109.    GS_dBase_dRec = ^GS_dBase_DataRecord;
  110.            {Pointer type used in object descriptions to locate the memory}
  111.            {array in bytes for the dBase record.  Uses GS_dBase_DataRecord}
  112.            {defined below.}
  113.  
  114.    GS_dBase_DataRecord = ARRAY[0..GS_dBase_MaxRecBytes] OF Byte;
  115.            {Defines an array of bytes in memory that is as large as the }
  116.            {maximum size of a dBase record (GS_dBase_MaxRecBytes).}
  117.  
  118. {
  119.         ┌──────────────────────────────────────────────────────────────────┐
  120.         │  ********         Data Structure Description         **********  │
  121.         │                                                                  │
  122.         │  The following record defines the dBase III file header.  Refer  │
  123.         │  to Appendix A for an explanation of each data element.          │
  124.         └──────────────────────────────────────────────────────────────────┘
  125. }
  126.    GS_dBase_Head = Record
  127.                       DBType     : Byte;
  128.                       Year       : Byte;
  129.                       Month      : Byte;
  130.                       Day        : Byte;
  131.                       RecCount   : LongInt;
  132.                       Location   : Integer;
  133.                       RecordLen  : Integer;
  134.                       Reserved   : Array[1..20] of Byte;
  135.                    end;
  136.  
  137. {
  138.      ┌──────────────────────────────────────────────────────────────────┐
  139.      │  *********             Field Descriptor              *********   │
  140.      │                                                                  │
  141.      │  This record defines the field descriptor.  There is one of      │
  142.      │  these for each field defined in the database structure.  They   │
  143.      │  are stacked as 32 bytes following the file header record, as    │
  144.      │  described in Appendix A.                                        │
  145.      └──────────────────────────────────────────────────────────────────┘
  146. }
  147.  
  148.    GS_dBase_Field = Record
  149.                        FieldName    : String[10];
  150.                                       {Array[1..11] of Char actually}
  151.                                       {This is to simplify conversion}
  152.                        FieldType    : Char;
  153.                        FieldAddress : LongInt;
  154.                        FieldLen     : Byte;
  155.                        FieldDec     : Byte;
  156.                        Reserved     : Array[1..14] of Char;
  157.                     end;
  158.  
  159.    GS_dBase_dFld = ^GS_dBase_DataField;
  160.           {Pointer type used in object descriptions to assign memory}
  161.           {for storing the field descriptors.                          }
  162.  
  163.    GS_dBase_DataField = ARRAY[1..GS_dBase_MaxRecField] OF GS_dBase_Field;
  164.           {Defines an array of field descriptors (GS_dBase_Field) that}
  165.           {is as large as the maximum number of dBase fields allowed}
  166.           {(GS_dBase_MaxRecFields).}
  167.  
  168.    GS_dBase_nFld = ^GS_dBase_NameField;
  169.           {Pointer type used in object descriptions to assign memory}
  170.           {for storing the field name strings.                      }
  171.  
  172.    GS_dBase_NameField = Array[1..GS_dBase_MaxRecField] OF string[11];
  173.           {Defines an array of field name strings (GS_dBase_Field) that}
  174.           {is as large as the maximum number of dBase fields allowed}
  175.           {(GS_dBase_MaxRecFields).}
  176.  
  177.  
  178. {
  179.        ┌──────────────────────────────────────────────────────────────┐
  180.        │  ***********      dBase Object Definition      ************  │
  181.        └──────────────────────────────────────────────────────────────┘
  182. }
  183.  
  184.    GSP_dBase_DB = ^GS_dBase_DB;
  185.    GS_dBase_DB = object(GS_KeyI_Objt) {Make it a child for keyboard control}
  186.       FileName     : string[64];      {Stores FileName of dBase File}
  187.       dFile        : file;            {File Type to reference